﻿namespace JavaScript8000.Common
{
    /// <summary>
    /// Represent token which returned by scaner.
    /// </summary>
    public class Token
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Token"/> class.
        /// </summary>
        /// <param name="code">
        /// The code.
        /// </param>
        public Token(int code)
        {
            this.Code = code;
            this.UserCode = 0;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Token"/> class.
        /// </summary>
        /// <param name="code">
        /// The code.
        /// </param>
        /// <param name="userCode">
        /// The user code.
        /// </param>
        public Token(int code, int userCode)
        {
            this.Code = code;
            this.UserCode = userCode;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the code.
        /// </summary>
        public int Code { get; private set; }

        /// <summary>
        /// Gets the user code.
        /// </summary>
        public int UserCode { get; private set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The to string.
        /// </returns>
        public override string ToString()
        {
            return string.Format(
                "Code: {0}, Usercode: {1}, Value:{3}", 
                this.Code, 
                this.UserCode, 
                this.Code != SystemTokens.EofCode && this.Code != SystemTokens.IdCode
                && this.Code != SystemTokens.NumericLiteralCode && this.Code != SystemTokens.StringLiteralCode
                    ? SystemTokens.GetName(this.Code)
                    : "<user defined token>");
        }

        #endregion
    }
}